/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clientsimulator;

/**
 *
 * @author andrew
 */

import java.net.*;

import java.io.*;
public class TCPClient{



       ObjectInputStream Sinput;                // to read the socker

        ObjectOutputStream Soutput;        // towrite on the socket

        Socket socket;

        ClientR cr = new ClientR();
        ClientW cw = new ClientW();



        // Constructor connection receiving a socket number

        TCPClient(int port, String ip) {

                // we use "localhost" as host name, the server is on the same machine

                // but you can put the "real" server name or IP address

                try {

                       socket = new Socket(InetAddress.getByName(ip), port);
                       socket.setKeepAlive(true);

                }

                catch(Exception e) {

                        System.out.println("Error connectiong to server:" + e);

                        return;

               }

                System.out.println("Connection accepted " +

                                socket.getInetAddress() + ":" +

                                socket.getPort());



                /* Creating both Data Stream */

                try

                {

                        Sinput  = new ObjectInputStream(socket.getInputStream());

                        Soutput = new ObjectOutputStream(socket.getOutputStream());

                }

               catch (IOException e) {

                        System.out.println("Exception creating new Input/output Streams: " + e);

                        return;

                }
                try
                {
                    cr.start();
                    cw.start();
                }
                catch(Exception e)
                {
                    System.out.print(e);
                }
    }
    public class ClientR extends Thread{
        public void run()
        {
            boolean reading = true;
            while(reading)
            {
                try {

                        String response = (String) Sinput.readObject();

                        System.out.println("Read back from server: " + response);
                        //Sinput.reset();

                }

                catch(Exception e) {

                        System.out.println("Problem reading back from server: " + e);

                }
            }
        }
    }
    public class ClientW extends Thread{
        public void run(){
              boolean done = false;
              while(!done)
               {
                // now that I have my connection
                BufferedReader toServerPacket = new BufferedReader(new InputStreamReader(System.in));
                String test = new String();
                try
                {
                    test = toServerPacket.readLine();
                }
                catch(Exception e)
                {
                    System.out.print(e);
                }
                // send the string to the server

                System.out.println("Client sending \"" + test + "\" to server");

                try {

                       Soutput.writeObject(test);

                        Soutput.flush();

                }

                catch(IOException e) {

                        System.out.println("Error writting to the socket: " + e);

                        return;

                }

                // read back the answer from the server


       }
      }
    }
}